package expressions.slogo;

import java.util.List;
import parsing.Parser;
import parsing.Separates;
import parsing.Token;
import expressions.Operator;

/**
 * Subtracts two numbers.
 * 
 * Depending on the input, this expression might also be converted to a Negate
 * when it is parsed.
 */
@Token(regex = "\\-")
@Separates
public class Subtract extends Operator<Double>
{
    public Subtract()
    {
        super(2);
    }

    @Override
    public void parse(Parser<Double> parser)
    {

        if (parser.operatorExpected())
        {
            /* binary minus */
            super.parse(parser);
        }
        else
        {
            /* unary minus */
            Negate unaryMinus = new Negate();
            unaryMinus.parse(parser);
        }
    }

    @Override
    public int priority()
    {
        return Priority.ARITHMETIC.adjust(Modifier.LOWEST);
    }

    @Override
    protected Double combineResults(List<Double> results)
    {
        return results.get(0) - results.get(1);
    }

}
